package org.apache.ocean;

import java.io.File;
import java.math.BigInteger;

import org.apache.ocean.util.FileSequence;

public class CSequence {
	public String name;
	public File directory;
	private FileSequence fileSequence;

	public static void main(String[] args) {
		try {
			CSequence sequence = new CSequence("testsequence", new File("g:\\dev"));
			for (int x=0; x < 10; x++) {
			  System.out.println("next: "+sequence.next());
			}
			sequence.set(new BigInteger("7"));
			System.out.println("next: "+sequence.next());
			System.out.println("next: "+sequence.next());
			System.out.println("next: "+sequence.next());
		} catch (Throwable throwable) {
      throwable.printStackTrace();
		}
	}

	public CSequence(String name, File directory) throws Exception {
		this.name = name;
		this.directory = directory;
		fileSequence = new FileSequence(directory.getAbsolutePath());
		fileSequence.create(name, new BigInteger("1"));
	}

	public BigInteger current() throws Exception {
		return fileSequence.read(name);
	}

	public void set(BigInteger value) throws Exception {
		fileSequence.delete(name);
		//BigInteger newValue = value.add(new BigInteger("1"));
		fileSequence.create(name, value);
		//fileSequence.setSequenceValue(name, value);
	}
  
	public CLong nextCLong() throws Exception {
		return new CLong(fileSequence.nextSequenceValueBottom(name, 1).longValue());
	}
	
	public BigInteger next() throws Exception {
		return fileSequence.nextSequenceValueBottom(name, 1);
	}
}
